package com.sgl.gk.controller;

import com.sgl.common.annotation.Log;
import com.sgl.common.core.controller.BaseController;
import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.core.page.TableDataInfo;
import com.sgl.common.enums.BusinessType;
import com.sgl.common.utils.ServletUtils;
import com.sgl.gk.domain.GkProduct;
import com.sgl.gk.service.GkProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:xec
 * @date:2022/10/31 18:50
 *
 * 产品管理的controller
 **/
@RestController
@RequestMapping("/gk/product")
public class GkProductController extends BaseController {

    @Autowired
    GkProductService gkProductService;

    /**
     * 按条件分页查询产品信息
     * @param gkProduct 查询条件
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(GkProduct gkProduct){
        if(ServletUtils.getParameter("pageSize")!=null&&ServletUtils.getParameter("pageNum")!=null){
            startPage();
        }
        List<GkProduct> list = gkProductService.list(gkProduct);
        return getDataTable(list);
    }

    /**
     * 添加产品信息
     * @param gkProduct
     * @return
     */
    @Log(title = "产品",businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody GkProduct gkProduct){
        if (gkProduct.getCode()==null||gkProduct.getCode()==""||gkProduct.getName()==null||gkProduct.getName()==""){
            return AjaxResult.error("产品名称和产品编码必须填写");
        }
        return gkProductService.add(gkProduct);
    }

    /**
     * 修改产品信息
     * @param gkProduct
     * @return
     */
    @Log(title = "产品",businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody GkProduct gkProduct){
        if (gkProduct.getCode()==null||gkProduct.getCode()==""||gkProduct.getName()==null||gkProduct.getName()==""){
            return AjaxResult.error("产品名称和产品编码必须填写");
        }
        return gkProductService.edit(gkProduct);
    }

    /**
     * 删除产品信息
     * @param ids
     * @return
     */
    @Log(title = "产品" ,businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult removeByIds(@PathVariable("ids") Long[] ids){
        return gkProductService.removeByIds(ids);
    }
}
